import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/base/Refresher.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/pages/contact/model/ContactItemWithPY.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';
import 'package:huajianshe_app/widget/appbar/ContactAppBar.dart';

import 'model/ContactsModel.dart';


//人脉首页
class ContactPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _ContactPageState();
  }
}

class _ContactPageState extends BaseState<ContactPage> {

  List<ContactListData>? data;
  bool showRadio = false;
  int action = -1;
  bool showBottom = false;

  @override
  void initState() {
    super.initState();
    refresher = Refresher<ContactListData>(this);
    refresher?.enablePullUp = false;
    sendReq(Api.contactHome(), ContactListResp(), data: {}, doNotShowProgressBar: false);
  }

  @override
  void onRefresh() {
    refresher?.mode = Mode.refresh;
    sendReq(Api.contactHome(), ContactListResp(), data: {}, doNotShowProgressBar: true);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is ContactListResp) {
      this.data = t.data;
      refresher?.load(t.data);
      setState(() => null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: ContactAppBar(onSelected),
      body: Stack(children: [
        refresher?.build(buildChild()) ?? H(0),
        if (showBottom) Positioned(bottom: 80, left: 0, right: 0,
            child: Align(alignment: FractionalOffset.bottomCenter, child: buildBottom())
        )
      ])
    );
  }

  Widget buildChild() {
    Widget? child = buildEmptyPage();
    if (child != null) {
      return buildHeader();
    }
    return
      ListView.separated(
          itemBuilder: (_, index) {
            // print('itemBuilder:$index');
             return index == 0 ? buildHeader() : ContactItemWithPY(this.data?[index - 1],
               padding: EdgeInsets.symmetric(horizontal: 13, vertical: 10),);
          },
          separatorBuilder: (_, index) { return H(3); },
          itemCount: this.data?.length ?? 0
      );
  }

  Widget buildHeader() {
    return
      Column(mainAxisSize: MainAxisSize.min, children: [
        H(10),
        Padding(padding: EdgeInsets.symmetric(horizontal: 15), child: GestureDetector(onTap: () => search(),
            child: RoundedCornerWidget(
                backgroundColor: C.grayF6, borderColor: Colors.transparent, height: 40, radius: 13,
                child: Row(mainAxisSize: MainAxisSize.max, children: [
                  W(16),
                  LoadImage('ic_gray_search'),
                  W(12),
                  Text('搜索圈子名称、群名称、人名', style: TS(16, C.gray666),)
                ])
            )
        )),
        H(15),
        Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
          ColoredBox(color: Colors.transparent, child: SmartButton(height: 77, text: '我的群聊', textColor: C.gray333, fontSize: 13, backgroundColor: Colors.transparent,
            borderColor: Colors.transparent, borderWidth: 0, drawableTop: LoadImage('ic_my_chat_group'), drawablePadding: 8, onPressed: myChatGroup,)),
          SmartButton(height: 77, text: '我的标签', textColor: C.gray333, fontSize: 13, backgroundColor: Colors.transparent,
            borderColor: Colors.transparent, drawableTop: LoadImage('ic_my_labels'), drawablePadding: 8,),
        ]),
        H(10),
        RoundedCornerWidget(child: W(0), borderWidth: 0, radius: 13, cornerPosition: CornerPosition.LB | CornerPosition.RB, height: 13, width: double.infinity,
        backgroundColor: Colors.white, boxShadowList: [
            BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.07), offset: Offset(0, 0), blurRadius: 13),
            BoxShadow(color: Colors.white, offset: Offset(0, -10)),//干掉上方的阴影
        ],),
        H(10),
      ]);
  }

  Widget buildBottom() {
    return ColoredBox(color: Colors.transparent, child: SafeArea(top: false, child: Center(child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisSize: MainAxisSize.max, children: [
      SmartButton(width: 130, height: 44, text: '提交', textColor: Colors.white, fontSize: 15,
      backgroundColor: C.primary, radius: 22, onPressed: submit,),
      W(13),
      SmartButton(width: 130, height: 44, text: '取消', textColor: C.primary, fontSize: 15,
      backgroundColor: Colors.white, radius: 22, onPressed: cancel,)
    ]))));
  }

  onSelected(index) {
    print('${index}');
    action = index;
    switch(index) {

    }
    showBottom = true;
    showRadioOrNot(true);

  }

  search() {

  }

  submit() {

  }

  myChatGroup() {
    print('myChatGroup');
    showRadio = true;
    this.data?.forEach((element) {
      element.data?.forEach((e2) {
        e2.showRadio = true;
      });
    });
    setState(() => null);
  }

  cancel() {
    showBottom = false;
    showRadioOrNot(false);
  }

  showRadioOrNot(bool show) {
    this.data?.forEach((element) {
      element.data?.forEach((e2) {
        e2.showRadio = show;
      });
    });
    setState(() => null);
  }
}